<?php
Class SP_Helper_Html
{
	public static function makeTagWithAttr($tag,$attrs)
	{
		return '<'.$tag.self::makeAttributes($attrs).'>';
	}
	
	public static function makeAttributes($attrs)
	{
		if(is_array($attrs))
		foreach ($attrs as $key => $attr)
		{
			$out.=' '.$key.'="'.$attr.'"';
		}
		return $out;
	}
	
	public static function makeInTags($tag,$array)
	{
		$open="<$tag>";
		$close="</$tag>";
		if(is_array($array))
		foreach ($array as $value)
		{
			$out.=$open.$value.$close."\n";
		}
		return $out;
	}
	
	public static function makeTr()
	{
		$out=NULL;
		$arguments = func_get_args();
		$count=count($arguments);
		if ($count==1)
		{
			$values=$arguments[0];
			if(is_array($values))
			{
				$out="<tr>\n".self::makeInTags('td',$values)."</tr>\n";
			}
		}
		elseif ($count==2)
		{
			$values=$arguments[0];
			$options=$arguments[1];
			if(is_array($values) && is_array($options))
			{
				$out=self::makeTagWithAttr('tr',$options)."\n".self::makeInTags('td',$values)."</tr>\n";
			}
		}
		elseif($count>2)
		{
			if(is_array($arguments[$count-1]))
			{
				$options=$arguments[$count-1];
				$values=array_slice($arguments, 0, $count-1); 
				$out=self::makeTagWithAttr('tr',$options)."\n".self::makeInTags('td',$values)."</tr>\n";
			}
			else
			{
				$out="<tr>\n".self::makeInTags('td',$arguments)."</tr>\n";
			}
		}
		return $out;
	}
	
	public static function makeUri($route_to_uri,$route_params=array(),$absolute=false)
	{
		if( empty($route_params) or $route_params=='') $route_params=array();
		if ( is_array($route_to_uri) )
		{
			if ( empty($route_to_uri['module']) )  throw new Exception('Can not make uri module not defined');
			$uri='/'.$route_to_uri['module'];
			unset($route_to_uri['module']);
			if ( isset($route_to_uri['action']) ) $uri.='/'.$route_to_uri['action'];
			unset($route_to_uri['action']);
			foreach($route_to_uri as $key => $value)
			{
				$uri.="/$key/$value";
			}
		}
		else
		{
			if(strpos($route_to_uri,':') === false)
			{
				$uri=$route_to_uri;
			}
			else
			{
				$route_name=substr($route_to_uri, 1);
				if(!SP_Routing_RouteCollector::has($route_name)) throw new Exception('Can not make uri for route "'.$route_name.'", route not found');
				$route=SP_Routing_RouteCollector::get($route_name);
				$router = new SP_Routing_Router;
				$uri=$router->generateUriFromRoute($route,$route_params);
			}
		}
		
		if(SP_Config_Configurator::get('app')->show_script_name)
		{
			$uri=SP_Config_Configurator::get('app')->script_name.$uri;
		}
		$uri='/'.$uri;
		if ( $absolute and SP_Config_Configurator::get('app')->site)
		{
			$uri=SP_Config_Configurator::get('app')->site.$uri;
		}
		return $uri;
	}
	
	public static function makeLink2($title, $href, $attr=array())
	{
		if (isset($attr['confirm']))
		{
			$attr['onClick']="return window.confirm('".$attr['confirm']."')" ;
			unset($attr['confirm']);
		}
		$out='<a href="'.$href.'"'.self::makeAttributes($attr).'>'.$title.'</a>';
		return $out;
	}
	
	public static function makeLink($name,$route_to_uri,$options=null,$route_params=null,$absolute=false)
	{
		$href=self::makeUri($route_to_uri,$route_params,$absolute);
		if( empty($options) or $options=='') $options=array();
		
		if (isset($options['confirm']))
		{
			$options['onClick']="return window.confirm('".$options['confirm']."')" ;
			unset($options['confirm']);
		}
		return '<a href="'.$href.'"'.self::makeAttributes($options).'>'.$name.'</a>';

		/*
		$arguments = func_get_args();
		if (count($arguments)==2)
		{
			$name=$arguments[0];
			$href=$arguments[1];
			return '<a href="'.$href.'">'.$name.'</a>';
		}
		elseif(count($arguments)==3)
		{
			$name=$arguments[0];
			$href=$arguments[1];
			$options=$arguments[2];
			if (!is_array($options))
			{
				throw new Exception('Third argument must be array!');
			}
			if (isset($options['confirm']))
			{
				$options['onClick']="return window.confirm('".$options['confirm']."')" ;
				unset($options['confirm']);
			}
			return '<a href="'.$href.'"'.self::makeAttributes($options).'>'.$name.'</a>';
		}
		else
		{
			throw new Exception('Invalid arguments count. It must be 2 or 3!');
		}	
		*/
	}
	
	public static function makeImage()
	{
		$arguments = func_get_args();
		if (count($arguments)==2)
		{
			$src=$arguments[0];
			$alt=$arguments[1];
			return '<img src="'.$src.'" alt="'.$alt.'" />';
		}
		elseif(count($arguments)==3)
		{
			$src=$arguments[0];
			$alt=$arguments[1];
			$options=$arguments[2];
			if (!is_array($options))
			{
				throw new Exception('Third argument must be array!');
			}
			return '<img src="'.$src.'" alt="'.$alt.'"'.self::makeAttributes($options).' />';
		}
		else
		{
			throw new Exception('Invalid arguments count. It must be 2 or 3!');
		}	
	}
	
}
?>